﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Program16
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 画直线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLine_Click(object sender, EventArgs e)
        {
            //创建GDI对象
            Graphics g = this.CreateGraphics();

            //创建画笔对象
            Pen pen = new Pen(Color.Crimson,4.0F);
            //画直线，建立坐标
            Point p1 = new Point(30, 50);
            Point p2 = new Point(600, 50);

            //绘制直线
            g.DrawLine(pen, p1, p2);

        }

        private void btnRect_Click(object sender, EventArgs e)
        {
            //创建gdi对象
            Graphics g = this.CreateGraphics();
            Pen  pen = new Pen(Brushes.Black);

            //定义矩形大小
            Size size = new Size(100, 80);
            //开始定义坐标
            Point point = new Point(30, 50);
            //定义矩形
            Rectangle rect = new Rectangle(point, size);

            g.DrawRectangle(pen, rect);
        }

        private void btnCircle_Click(object sender, EventArgs e)
        {
            //创建GDI对象
            Graphics g = this.CreateGraphics();
            Pen pen = new Pen(Brushes.Coral, 6.0F);
            g.DrawEllipse(pen, 90, 90, 200, 100);
        }

        private void btnsan_Click(object sender, EventArgs e)
        {
            //创建GDI对象
            Graphics g = this.CreateGraphics();

            //创建画笔对象
            SolidBrush brush = new SolidBrush(Color.Crimson);

            g.FillPie(brush, 50, 100, 200, 200, 45, 90);

            brush = new SolidBrush(Color.Aqua);

            g.FillPie(brush, 50, 100, 200, 200, 135, 90);

            brush = new SolidBrush(Color.Red);

            g.FillPie(brush, 50, 100, 200, 200, 225, 90);
            brush = new SolidBrush(Color.Blue);

            g.FillPie(brush, 50, 100, 200, 200, 315, 90);

            //写字
            g.DrawString("年度业绩", new Font("微软雅黑", 14, FontStyle.Regular), Brushes.Blue, new Point(40, 350));

        }

        private void btnyzm_Click(object sender, EventArgs e)
        {

            SetCode();
            
        }

        /// <summary>
        /// 绘制验证码的方法
        /// </summary>
        void SetCode()
        {
            //默认验证码
            string code = "sd156csasdf";
            code = code.Trim();
            if (String.IsNullOrEmpty(code))
            {
                return;
            }
            //验证码大小宽65，高20
            Bitmap image = new Bitmap(100, 25);
            //创建绘图对象Graphics
            Graphics graphics = Graphics.FromImage(image);
            //随机
            Random r = new Random();
            graphics.Clear(Color.White);
            //绘制四条随机出现的线
            for (int i = 0; i < 4; i++)
            {
                int x1 = r.Next(image.Width);
                int y1 = r.Next(image.Height);

                int x2 = r.Next(image.Width);
                int y2 = r.Next(image.Height);
                graphics.DrawLine(new Pen(Brushes.Black, 1), new Point(x1, y1), new Point(x2, y2));
            }

            for (int i = 0; i < 10; i++)
            {
                int x1 = r.Next(image.Width);
                int y1 = r.Next(image.Height);

                graphics.DrawArc(new Pen(Brushes.Blue),x1,y1,100,50,200,50);
            }

            for (int i = 0; i < 10; i++)
            {
                int y1 = r.Next(image.Height);
                int x1 = r.Next(image.Width);

                graphics.DrawArc(new Pen(Brushes.Green), x1, y1, 100, 50, 200, 50);
            }

            this.BackgroundImage = image;
            this.BackgroundImageLayout = ImageLayout.Center;
            graphics.DrawRectangle(new Pen(Brushes.Black), 0, 0, image.Width - 1, image.Height - 1);
            graphics.DrawString(code, new Font("微软雅黑",14, FontStyle.Bold), Brushes.Red, 8, -2);
        }

        private void label1_Click(object sender, EventArgs e)
        {
            label1.Text = ValidateCode.CreateVCode(4);

            //把图片读入内存
            MemoryStream ms = new MemoryStream();
            ms.Write(ValidateCode.CreateVCodePicture(label1.Text), 0, ValidateCode.CreateVCodePicture(label1.Text).Length);
            //显示图片的文件流格式
            pb_vCode.Image = Image.FromStream(ms);

        }
    }
}
